/**
 * Created by zhoua on 2017/8/8.
 */
import axios from 'axios';
// import store from '../vuex'
// import router from '../router'
import { Toast } from 'mint-ui';
let baseURL = '/sharing';

if (process.env.NODE_ENV === 'production') {
  baseURL = 'api';
} else {
  baseURL = 'zgsharing/api';
};

export function fetch (options, json) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout: 10000,
      baseURL,
      headers: json ? { 'Content-Type': 'application/x-www-from-urlencoded;charset=utf-8' } : { 'Content-Type': 'application/json' }
    });
    instance(options)
      .then(response => {
        let res = response.data;
        if (res.resultCode === '0') {
          resolve(res);
        } else {
          if (res.resultCode) {
            Toast(res.resultDetail);
          } else {
            Toast('服务器错误');
          }
          reject(res);
        }
      })
      .catch(error => {
        if (!error) return;
        reject(error);
      })
  })
}

export function fetchForLocation (options, json) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout: 10000,
      baseURL,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      withCredentials: true
    });
    instance(options)
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        if (!error) return;
        reject(error);
      })
  })
}
